import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;

public class Data extends UnicastRemoteObject implements RIData {

	private static final String DATA_NAME = "Data";

	protected ArrayList vStudent;

	protected ArrayList vCourse;

	public Data(String sStudentFileName, String sCourseFileName) throws RemoteException, FileNotFoundException, IOException {
		BufferedReader objStudentFile = new BufferedReader(new FileReader(sStudentFileName));
		BufferedReader objCourseFile = new BufferedReader(new FileReader(sCourseFileName));

		this.vStudent = new ArrayList();
		this.vCourse = new ArrayList();

		while (objStudentFile.ready()) {
			this.vStudent.add(new Student(objStudentFile.readLine()));
		}
		while (objCourseFile.ready()) {
			this.vCourse.add(new Course(objCourseFile.readLine()));
		}

		objStudentFile.close();
		objCourseFile.close();
	}

	public synchronized ArrayList getAllStudentRecords() {
		return this.vStudent;
	}

	public synchronized ArrayList getAllCourseRecords() {
		return this.vCourse;
	}

	public synchronized Student getStudentRecord(String sSID) {
		for (int i = 0; i < this.vStudent.size(); i++) {
			Student objStudent = (Student) this.vStudent.get(i);
			if (objStudent.match(sSID)) {
				return objStudent;
			}
		}

		return null;
	}

	public synchronized String getStudentName(String sSID) {
		for (int i = 0; i < this.vStudent.size(); i++) {
			Student objStudent = (Student) this.vStudent.get(i);
			if (objStudent.match(sSID)) {
				return objStudent.getName();
			}
		}

		return null;
	}

	public synchronized Course getCourseRecord(String sCID) {
		for (int i = 0; i < this.vCourse.size(); i++) {
			Course objCourse = (Course) this.vCourse.get(i);
			if (objCourse.match(sCID)) {
				return objCourse;
			}
		}

		return null;
	}

	public synchronized String getCourseName(String sCID) {
		for (int i = 0; i < this.vCourse.size(); i++) {
			Course objCourse = (Course) this.vCourse.get(i);
			if (objCourse.match(sCID)) {
				return objCourse.getName();
			}
		}

		return null;
	}

	public synchronized void makeARegistration(String sSID, String sCID) {
		Student objStudent = this.getStudentRecord(sSID);
		Course objCourse = this.getCourseRecord(sCID);

		if (objStudent != null && objCourse != null) {
			objStudent.registerCourse(objCourse);
			objCourse.registerStudent(objStudent);
		}
	}

	public static void main(String args[]) {
		if (args.length != 2) {
			System.err.println("Numar incorect de parametrii");
			System.err.println("Utilizare: java Data <FisierStudenti> <FisierCursuri>");
			System.exit(1);
		}

		if (!new File(args[0]).exists()) {
			System.err.println("Nu exista fisierul " + args[0]);
			System.exit(1);
		}
		if (new File(args[1]).exists() == false) {
			System.err.println("Nu exista fisierul " + args[1]);
			System.exit(1);
		}

		try {
			Data objData = new Data(args[0], args[1]);
			Naming.bind(DATA_NAME, objData);
			System.out.println("Nodul datelor este gata pentru servire.");

			System.out.println("Apasati Enter pentru terminare.");
			System.in.read();

			Naming.unbind(DATA_NAME);
			System.out.println("Nodul datelor se dezactiveaza. Apasati Ctrl-C daca dureaza prea mult.");
		} catch (java.rmi.ConnectException e) {
			System.err.println("Java RMI error: verificat daca rmiregistry este pornit.");
			System.exit(1);
		} catch (Exception e) {
			System.err.println("Unexpected exception at " + DATA_NAME);
			e.printStackTrace();
			System.exit(1);
		}
	}

}